﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AwesomeEnginePC.Game.Screens
{
    /// <summary>
    /// Describes a manager to keep track of different screens. The generic argument for this class must be an enum describing the different states for the screens.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ScreenManager<T> where T : struct, IConvertible
    {
        public Dictionary<T, BaseScreen<T>> Screens { get; protected set; }

        private T currentState;
        public T CurrentState
        {
            get { return currentState; }
            set
            {
                currentState = value;
                if (Screens[currentState] != null)
                {
                    Screens[currentState].ResetScreen();
                }
            }
        }
        //public T PreviousState { get; protected set; }
        
        public bool ScreenManagerComplete { get; set; }

        public ScreenManager()
        {
            if(!typeof(T).IsEnum)
                throw new ArgumentException("Generic argument type must be an enum.");

            Screens = new Dictionary<T, BaseScreen<T>>();
        }

        public void AddScreen(T state, BaseScreen<T> screen)
        {
            Screens.Add(state, screen);
        }

        public void RemoveScreen(T state)
        {
            Screens.Remove(state);
        }

        public void Update(GameTime gameTime)
        {
            //PreviousState = CurrentState;

            if (Screens[CurrentState] != null)
            {
                Screens[CurrentState].Update(gameTime);

                if (Screens[CurrentState].ScreenComplete)
                {
                    if (Screens[CurrentState].NextScreen == null)
                    {
                        ScreenManagerComplete = true;
                    }
                    else
                    {
                        CurrentState = Screens[CurrentState].NextScreen.Value;
                    }

                }
                    
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (Screens[CurrentState] != null)
                Screens[CurrentState].Draw(gameTime, spriteBatch);
        }

    }
}
